import java.util.Arrays;

public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack(){
        this.elem = new int[10];
    }

    public boolean isFull(){
        return this.elem.length == usedSize;
    }

    //将 元素 val 入栈 , 并返回
    public void push(int val){
        if(isFull()){//判满
            this.elem = Arrays.copyOf(elem,elem.length*2);//满了则扩大为原来的两倍
        }
        this.elem[usedSize++] = val;//后置++ , 先使用usedSize , 再+1
    }

    public boolean empty(){
        return usedSize == 0;//判断是否为空 , 空返回true , 非空返回false
    }
    public int pop(){//将栈顶元素取出 , 并返回
        if(empty()){
            throw new EmptyStackException();
        }else{
            int val = elem[usedSize-1];
            usedSize--;
            return val;
        }
    }
    public int peek(){//获取栈顶元素
        if(empty()){
            throw new EmptyStackException();
        }else{
            return elem[usedSize];
        }
    }

    @Override
    public String toString() {
        return
                 Arrays.toString(elem)
                ;
    }
}
